package com.zzyl.controller;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.PostDto;
import com.zzyl.dto.RoleDto;
import com.zzyl.service.PostService;
import com.zzyl.service.RoleService;
import com.zzyl.vo.PostVo;
import com.zzyl.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;


@Slf4j
@Api(tags = "角色管理")
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    RoleService roleService;


    @PostMapping("/page/{pageNum}/{pageSize}")
    @ApiOperation(value = "角色分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleDto", value = "角色DTO对象", required = true, dataType = "RoleDto"),
            @ApiImplicitParam(paramType = "path", name = "pageNum", value = "页码", example = "1", dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "pageSize", value = "每页条数", example = "10", dataType = "Integer")
    })
    public ResponseResult<PageResponse<RoleVo>> findPostVoPageResponse(
            @RequestBody RoleDto roleDto,
            @PathVariable("pageNum") int pageNum,
            @PathVariable("pageSize") int pageSize) {
        PageResponse<RoleVo> pageResponse = roleService.findrolePage(roleDto, pageNum, pageSize);
        return ResponseResult.success(pageResponse);
    }


    @PutMapping
    @ApiOperation(value = "角色添加")
    @ApiImplicitParam(name = "roleDto", value = "角色DTO对象", required = true, dataType = "RoleDto")
    public ResponseResult<RoleVo> AddRole(@RequestBody RoleDto roleDto) {
        Boolean flag = roleService.AddRole(roleDto);
        return ResponseResult.success(flag);
    }


    @GetMapping("/find-checked-resources/{roleId}")
    @ApiOperation(value = "根据角色id查询")
    public ResponseResult<Set<String>> findCheckedResources(@PathVariable Long roleId) {
        log.info("接受id的数据：{}", roleId);
        Set<String> resources = roleService.findCheckedResources(roleId);

        return ResponseResult.success(resources);
    }

    @PatchMapping
    @ApiOperation(value = "角色修改")
    public ResponseResult<Boolean> updatarole(@RequestBody RoleDto roleDto) {

        log.info("根据角色修改的数据：{}，{}", roleDto, roleDto.getId());

        Boolean flage = roleService.updatarole(roleDto);

        return ResponseResult.success(flage);
    }

    @DeleteMapping("/{roleIds}")
    @ApiOperation(value = "根据角色id删除")
    public ResponseResult deleteByroleIds(@PathVariable List<String> roleIds) {

        log.info("角色修改的数据：{}", roleIds);

        roleService.deleteByroleIds(roleIds);

        return ResponseResult.success();
    }
   @PostMapping("/init-roles")
    @ApiOperation(value = "角色下拉框")
    public ResponseResult<List<RoleVo>> seletedropDownBox(@RequestBody RoleDto roleDto) {
//角色下拉框的查询
       List<RoleVo> roleVos = roleService.seletedropDownBox(roleDto);
       return ResponseResult.success(roleVos);



    }
}
